package com.ieng.ormosia.web.controller.system;

import com.ieng.ormosia.common.exception.PrimaryKeyException;
import com.ieng.ormosia.common.utils.ActionR;
import com.ieng.ormosia.common.vo.ResponseData;
import com.ieng.ormosia.connect.model.Permission;
import com.ieng.ormosia.connect.service.PermissionService;
import io.swagger.v3.oas.annotations.Operation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/system/permission")
public class PermissionController {
	private final PermissionService permissionService;

	public PermissionController(PermissionService permissionService) {
		this.permissionService = permissionService;
	}

	@Operation(summary = "权限列表")
	@RequestMapping(value = "/list")
	public ResponseData list(){
		return ActionR.success(permissionService.getPermissionList());
	}

	@Operation(summary = "查询权限")
	@RequestMapping(value = "/query")
	public ResponseData query(@RequestBody(required = false) Permission permission){
		return ActionR.success(permissionService.getPermissionHashQ(permission));
	}

	@Operation(summary = "权限回收站")
	@RequestMapping(value = "/recycle")
	public ResponseData recycle(){
		return ActionR.success(permissionService.getPermissionDelAll());
	}

	@Operation(summary = "物理删除权限")
	@DeleteMapping(value = "/del/{ids}")
	public ResponseData del(@PathVariable("ids") Long[] ids){
		permissionService.delPermission(ids);
		return ActionR.send("删除成功");
	}

	@Operation(summary = "根据ID查询权限信息")
	@GetMapping
	public ResponseData get(@RequestParam("id") Long id){
		return ActionR.success(permissionService.getPermission(id));
	}

	@Operation(summary = "添加权限")
	@PostMapping
	public ResponseData add(@Validated @RequestBody Permission permission){
		permissionService.addPermission(permission);
		return ActionR.send("新增成功");
	}

	@Operation(summary = "修改权限")
	@PutMapping
	public ResponseData edit(@RequestBody Permission permission){
		if(permission.getId() == null){
			throw new PrimaryKeyException("permission id is null");
		}
		permissionService.editPermission(permission);
		return ActionR.send("修改成功");
	}

	@Operation(summary = "逻辑删除权限")
	@DeleteMapping("/{ids}")
	public ResponseData remove(@PathVariable("ids") Long[] ids){
		permissionService.removePermission(ids);
		return ActionR.send("删除成功");
	}

}
